package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.utils.SPUtils;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.ui.EaseChatFragment;
import com.hyphenate.easeui.widget.chatrow.EaseCustomChatRowProvider;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChatActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.image_emoj)
    ImageView imageEmoj;
    @BindView(R.id.rl_bottom)
    RelativeLayout rlBottom;
    @BindView(R.id.image1)
    ImageView image1;
    @BindView(R.id.rl_emoj)
    RelativeLayout rlEmoj;
    @BindView(R.id.container)
    FrameLayout container;
    @BindView(R.id.title_right_one_btn)
    ImageView titleRightOneBtn;
    private String to_user_id;
    private String userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_chat);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        userId= (int) SPUtils.get(this, Constant.USER_ID,0)+"";
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        //new出EaseChatFragment或其子类的实例
        EaseChatFragment chatFragment = new EaseChatFragment();
        chatFragment.setChatFragmentHelper(new EaseChatFragment.EaseChatFragmentHelper() {
            @Override
            public void onSetMessageAttributes(EMMessage message) {

            }

            @Override
            public void onEnterToChatDetails() {

            }

            @Override
            public void onAvatarClick(String username) {
                if(TextUtils.equals(userId,username)){
                    return;
                }
                Intent i=new Intent(ChatActivity.this, AttentionUser2Activity.class);
                i.putExtra("listId",username);
                startActivity(i);
//                ToastUtils.toastForShort(ChatActivity.this,username);
            }

            @Override
            public void onAvatarLongClick(String username) {

            }

            @Override
            public boolean onMessageBubbleClick(EMMessage message) {
                return false;
            }

            @Override
            public void onMessageBubbleLongClick(EMMessage message) {

            }

            @Override
            public boolean onExtendMenuItemClick(int itemId, View view) {
                return false;
            }

            @Override
            public EaseCustomChatRowProvider onSetCustomChatRowProvider() {
                return null;
            }
        });
        //传入参数
        Bundle args = new Bundle();
        args.putInt(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_SINGLE);
        args.putString(EaseConstant.EXTRA_USER_ID, getIntent().getStringExtra(EaseConstant.EXTRA_USER_ID));
        args.putString("to_headportrait", getIntent().getStringExtra("to_headportrait"));
        args.putString("to_username", getIntent().getStringExtra("to_username"));
        tvMiddleTitle.setText(getIntent().getStringExtra("to_username"));
        to_user_id=getIntent().getStringExtra(EaseConstant.EXTRA_USER_ID);
//        to_user_id=getIntent().getStringExtra("to_user_id");
        tvMiddleTitle.setVisibility(View.VISIBLE);
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        titleRightOneBtn.setVisibility(View.VISIBLE);
        titleRightOneBtn.setImageResource(R.mipmap.icon_wdguanyuwm);
        chatFragment.setArguments(args);
        getSupportFragmentManager().beginTransaction().add(R.id.container, chatFragment).commit();
    }
    @OnClick({R.id.title_left_one_btn, R.id.image_emoj, R.id.image1,R.id.title_right_one_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.image_emoj:
                rlEmoj.setVisibility(View.VISIBLE);
                rlBottom.setVisibility(View.GONE);
                break;
            case R.id.image1:
                rlBottom.setVisibility(View.VISIBLE);
                rlEmoj.setVisibility(View.GONE);
                break;
            case R.id.title_right_one_btn:
                Intent i=new Intent(this, AttentionUser2Activity.class);
                i.putExtra("listId",to_user_id);
                startActivity(i);
                break;
        }
    }
}
